package com.qs.user.pay;

import android.content.Context;

import com.qs.user.pay.util.GeneralUtils;
import com.qs.user.pay.util.PayCallBack;
import com.qs.user.pay.util.WXConstants;
import com.tsy.sdk.pay.alipay.Alipay;
import com.tsy.sdk.pay.weixin.WXPay;

import java.util.LinkedHashMap;
import java.util.Map;

import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;
/**
* 支付（微信，支付宝）
* @Author ltzz
* @Date 2019/8/26
*/
public class PayUtil {

    /**
     * 微信支付
     * @param prepayId
     * @param context
     * @param payCallBack 支付完成回调
     */
    public static void doWXPay(String prepayId, Context context, final PayCallBack payCallBack) {

        long timestamp = GeneralUtils.genTimeStamp();
        String nonceStr = GeneralUtils.genNonceStr();
        String packageSign = "Sign=WXPay";
        Map<String, String> map = new LinkedHashMap<>();
        map.put("appid", WXConstants.APP_ID);
        map.put("noncestr", nonceStr);
        map.put("package", packageSign);
        map.put("partnerid", WXConstants.PARTNER_ID);
        map.put("prepayid", prepayId);
        map.put("timestamp", timestamp + "");

        String sign = GeneralUtils.getPackageSign(map);
        KLog.e("sign: " + sign);

        String payParam = GeneralUtils.getJson(new String[]{"appid", "partnerid", "prepayid", "package", "noncestr", "timestamp", "sign"},
                new String[]{WXConstants.APP_ID, WXConstants.PARTNER_ID, prepayId, packageSign, nonceStr, timestamp + "", sign});
        KLog.e("doWXPay: " + payParam);

        WXPay.init(context, WXConstants.APP_ID);      // 要在支付前调用
        WXPay.getInstance().doPay(payParam, new WXPay.WXPayResultCallBack() {
            @Override
            public void onSuccess() {
                payCallBack.onSuccess();
                ToastUtils.showShort("支付成功");
            }

            @Override
            public void onError(int error_code) {
//                payCallBack.onSuccess();
                switch (error_code) {
                    case WXPay.NO_OR_LOW_WX:
                        ToastUtils.showShort("未安装微信或微信版本过低");
                        break;

                    case WXPay.ERROR_PAY_PARAM:
                        ToastUtils.showShort("参数错误");
                        break;

                    case WXPay.ERROR_PAY:
                        ToastUtils.showShort("支付失败");
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onCancel() {
//                payCallBack.onSuccess();
                ToastUtils.showShort("支付取消");
            }
        });
    }

    /**
     * 支付宝支付
     * @param payParam 支付服务生成的支付参数
     */
    public static void doAliPay(String payParam,Context context, final PayCallBack payCallBack) {
        new Alipay(context, payParam, new Alipay.AlipayResultCallBack() {
            @Override
            public void onSuccess() {
                payCallBack.onSuccess();
                ToastUtils.showShort("支付成功");
            }

            @Override
            public void onDealing() {
                ToastUtils.showShort("支付处理中...");
            }

            @Override
            public void onError(int error_code) {
//                payCallBack.onSuccess();
                switch (error_code) {
                    case Alipay.ERROR_RESULT:
                        ToastUtils.showShort("支付失败:支付结果解析错误");
                        break;

                    case Alipay.ERROR_NETWORK:
                        ToastUtils.showShort("支付失败:网络连接错误");
                        break;

                    case Alipay.ERROR_PAY:
                        ToastUtils.showShort("支付错误:支付码支付失败");
                        break;

                    default:
                        ToastUtils.showShort("支付错误");
                        break;
                }

            }

            @Override
            public void onCancel() {
//                payCallBack.onSuccess();
                ToastUtils.showShort("支付取消");
            }
        }).doPay();
    }
}
